<?php
//自动生成,请根据需要修改
namespace app\announcement\controller;

use app\common\controller\Base;
use think\Db;
use think\db\Where;

/**
 * @title 滚动公告页面（模块04-01）
 * @description 滚动公告说明
 * @group 公告（04）
 * @header name:model_id require:0 default:04-01 desc:模块
 * @groupremark 已使用错误码：1-04-01-0001 校验失败 1-04-01-0002 添加失败 1-04-01-0003 编辑校验失败 1-04-01-0004 编辑失败 1-04-01-0005 获取详情失败  1-04-01-0006 获取列表失败 1-04-01-0007获取单个详情失败 1-04-01-0008 删除失败
 */
class RollNotice extends Base
{
    //测试
    public function test()
    {
        return 'test 111';
    }

    /**
     * @title 添加滚动公告
     * @description 添加滚动公告
     * @author 李锐志
     * @url /announcement/roll_notice/add
     * @method *
     *
     * @header name:token require:1 default: desc:token
     *
     * @param name:content type:varchar require:0 other: desc:内容
     * @param name:create_time type:int require:0 other: desc:创建时间
     * @param name:over_time type:int require:0 other: desc:过期时间
     * @param name:status type:tinyint require:0 other: desc:状态
     * @return data:滚动公告@!
     * @data    id:id    content:内容    create_time:创建时间    over_time:过期时间    status:状态
     * @return error_id:1-04-01-0001 校验失败  1-04-01-0002 添加失败
     */
    public function add()
    {
        $data = input();
        $data['create_time'] = time();
        /**
         *$validate = new \app\announcement\validate\RollNotice();
         *$validate->scene('add');
         *$result = $validate->batch(true)->check($data);
         *if ($result !== true) {
         *$msg['msg']='校验失败';
         *$msg['validate']=$validate->getError();
         *$msg['error_id']='1-04-01-0001';
         *return $this->errorJson($msg);
         *}
         **/
        $model = new \app\announcement\model\RollNotice();
        $flag = $model->save($data);
        if ($flag) {
            $msg['msg'] = '添加成功';
            $msg['data'] = $model->get($model['id']);
            return $this->successJson($msg);
        } else {
            $msg['error_id'] = '1-04-01-0002';
            $msg['msg'] = '添加失败';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 编辑滚动公告
     * @description 编辑滚动公告
     * @author 李锐志
     * @url /announcement/roll_notice/edit
     * @method *
     *
     * @header name:token require:1 default: desc:token
     *
     * @param name:id type:int require:0 other: desc:id
     * @param name:content type:varchar require:0 other: desc:内容
     * @param name:create_time type:int require:0 other: desc:创建时间
     * @param name:over_time type:int require:0 other: desc:过期时间
     * @param name:status type:tinyint require:0 other: desc:状态
     * @return data:滚动公告@!
     * @data    id:id    content:内容    create_time:创建时间    over_time:过期时间    status:状态
     * @return error_id:1-04-01-0003 校验失败 1-04-01-0004 修改失败
     */
    public function edit()
    {
        $data = input();
        $validate = new \app\announcement\validate\RollNotice();
        $validate->scene('edit');
        $result = $validate->batch(true)->check($data);
        if ($result !== true) {
            $msg['msg'] = '校验失败';
            $msg['validate'] = $validate->getError();
            $msg['error_id'] = '1-04-01-0003';
            return $this->errorJson($msg);
        }
        $model = new \app\announcement\model\RollNotice();
        $flag = $model->edit($data);
        if ($flag !== false) {
            $msg['msg'] = '修改成功';
            $msg['data'] = $model->get($data['id']);
            return $this->successJson($msg);
        } else {
            $msg['error_id'] = '1-04-01-0004';
            $msg['msg'] = '修改失败';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 删除滚动公告
     * @description 删除滚动公告
     * @author 李锐志
     * @url /announcement/roll_notice/delete
     * @method *
     *
     * @param name:token require:1 default: desc:token
     *
     * @param name:id type:int|array require:1 other: desc:要删除的id
     * @return error_id:1-04-01-0008 删除失败
     */
    public function delete()
    {
        $id = input('id');
        $model = new \app\announcement\model\RollNotice();
        $flag = $model->destroy($id);
        if ($flag) {
            $msg['msg'] = '删除成功';
            return $this->successJson($msg);
        } else {
            $msg['msg'] = '删除失败';
            $msg['error_id'] = '1-04-01-0008';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 单个查询滚动公告
     * @description 单个查询滚动公告
     * @author 李锐志
     * @url /announcement/roll_notice/info
     * @method *
     *
     * @param name:token require:1 default: desc:token
     *
     * @param name:id type:int require:0 other: desc:id
     * @param name:content type:varchar require:0 other: desc:内容
     * @param name:create_time type:int require:0 other: desc:创建时间
     * @param name:over_time type:int require:0 other: desc:过期时间
     * @param name:status type:tinyint require:0 other: desc:状态
     * @return data:滚动公告@!
     * @data    id:id    content:内容    create_time:创建时间    over_time:过期时间    status:状态
     * @return error_id:1-04-01-0005 校验失败 1-04-01-0007 获取失败
     */
    public function info()
    {
        $model = new \app\announcement\model\RollNotice();
        $Fields = $model->getTableFields();
        $data = \think\facade\Request::only($Fields);
        /**
         *$validate = new \app\announcement\validate\RollNotice();
         *$validate->scene('info');
         *$result = $validate->batch(true)->check($data);
         */
        if (!$data) {
            $msg['msg'] = '必须有参数';
            $msg['error_id'] = '1-04-01-0005';
            return $this->errorJson($msg);
        }
        $model = new \app\announcement\model\RollNotice();
        $info = $model->where($data)->find();
        if ($info) {
            $msg['msg'] = '获取成功';
            $msg['data'] = $info;
            return $this->successJson($msg);
        } else {

            $msg['msg'] = '获取失败';
            $msg['error_id'] = '1-04-01-0007';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 查询列表滚动公告
     * @description 查询列表滚动公告
     * @author 李锐志
     * @url /announcement/roll_notice/get_list
     * @method *
     * @param name:token require:1 default: desc:token
     * @param name:page require:1 default: desc:第几页
     * @param name:pagesize require:1 default: desc:每页数
     * @return list:滚动公告@
     * @list    id:id    content:内容    create_time:创建时间    over_time:过期时间    status:状态
     * @return error_id:1-04-01-0006 校验失败
     */
    public function get_list()
    {
        $model = new \app\announcement\model\RollNotice();
        $Fields = $model->getTableFields();
        $data = \think\facade\Request::only($Fields);
        foreach($data as $k => $v){
            if($v === 0){
                continue;
            }
            if($v == null){
                unset($data[$k]);
            }
        }
        if(isset($data['content']) && $data['content'] && !empty($data['content'])){
            $data['content'] = ['like',"%{$data['content']}%"];
        }
        if(isset($data['status']) && $data['status'] && !empty($data['status'])){
            $data['status'] = ['=',"{$data['status']}"];
        }
        /**
         *$validate = new \app\announcement\validate\RollNotice();
         *$validate->scene('lists');
         *$result = $validate->batch(true)->check($data);
         *if ($result !== true) {
         *$msg['msg']='校验失败';
         *$msg['validate']=$validate->getError();
         *$msg['error_id']='1-04-01-0006';
         *return $this->errorJson($msg);
         *}
         **/
        $data = new Where($data);
        $list = $model->where($data)->order('id','desc')->paginate();
        foreach ($list as $k => $v) {
            if($v['roll_notice_type'] == 'activity'){
                $list[$k]['roll_notice_name'] = Db::table('zb_activity.tp_activity')->where('id',$v['roll_notice_id'])->value('title');
            }else{
                $list[$k]['roll_notice_name'] = Db::table('zb_topic.tp_topic')->where('id',$v['roll_notice_id'])->value('title');
            }
        }
        $msg['list'] = $list;
        return $this->successJson($msg);
    }

    /**
     * @title 前台获取滚动公告
     * @description 前台获取滚动公告
     * @author 李锐志
     * @url /announcement/roll_notice/get_roll_notice
     * @method *
     */
    public function get_roll_notice()
    {
        $result = Db::name('roll_notice')
            ->where('status', 1)
            ->where('over_time', '>', time())
            ->order('id', 'desc')
            ->limit(3)
            ->select();

        $msg['msg'] = '滚动公告获取成功';
        $msg['list'] = $result;
        return $this->successJson($msg);
    }

    /**
     * 后台滚动公告启用按钮
     */
    public function invoke()
    {
        $data = input();

        Db::name('roll_notice')
            ->where('id', $data['id'])
            ->setField('status', 1);

        $msg['msg'] = '滚动公告启用成功';
        return $this->successJson($msg);
    }

    /**
     * 后台滚动公告禁用按钮
     */
    public function forbidden()
    {
        $data = input();

        Db::name('roll_notice')
            ->where('id', $data['id'])
            ->setField('status', 0);

        $msg['msg'] = '滚动公告禁用成功';
        return $this->successJson($msg);
    }

    /**
     * 滚动公告选择跳转地址
     */
    public function notice_top()
    {
        $model = new \app\announcement\model\RollNotice();
        $Fields = $model->getTableFields();
        $data = \think\facade\Request::only($Fields);

        switch ($data['roll_notice_type'])
        {
            case 'activity':
                $list = Db::table('zb_activity.tp_activity')->where('status','1')->field('id,title')->select();
                break;
            default:
                $list = Db::table('zb_topic.tp_topic')->where('topic_type',$data['roll_notice_type'])->field('id,title')->select();
        }
        $msg['data'] = $list;
        return $this->successJson($msg);
    }
}
